package com.hdoit.kylin.web.studentManager.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.studentManager.model.Data;
import com.hdoit.kylin.logical.studentManager.model.ExperimentExcel;
import com.hdoit.kylin.logical.studentManager.service.IDataService;
import com.hdoit.kylin.logical.studentManager.service.IExperimentExcelService;
import com.hdoit.kylin.logical.viewmodel.DataSearch;
import com.hdoit.kylin.logical.viewmodel.ExperimentExcelSearch;

/**
 * @author admin
 * @email
 * @createtime 2017年07月21日 14:22:06
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/experimentExcel/")
public class ExperimentExcelController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/experimentExcel/";
	}

	private IExperimentExcelService experimentExcelService;
	private IDataService dataService;

	public IDataService getDataService() {
		return dataService;
	}
	@Resource
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}

	public IExperimentExcelService getExperimentExcelService() {
		return experimentExcelService;
	}

	@Resource
	public void setExperimentExcelService(
			IExperimentExcelService experimentExcelService) {
		this.experimentExcelService = experimentExcelService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "experimentExcelDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	
	private final String index2ViewName = "index2";
	private final String output2ViewName = "output2";
	private final String detailed2ViewName = "experimentDetailed2";
	

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			ExperimentExcelSearch search, boolean ajaxPost,String experimentId) {
		
		pagerModel.setTotalAmount(experimentExcelService.queryCountByExperimentId(search,experimentId));
		List<ExperimentExcel> experimentExcels = experimentExcelService
				.queryListByExperimentId(search, pagerModel,experimentId);
		//model.addAttribute("key", key);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("experimentExcels", experimentExcels);
		model.addAttribute("experimentExcelSearch", search);
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}

	@RequestMapping(value = index2ViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String index2(Model model, PagerModel pagerModel, ExperimentExcelSearch search, boolean ajaxPost,String experimentId) {
		
		pagerModel.setTotalAmount(experimentExcelService.queryCountByExperimentId(experimentId));
		List<ExperimentExcel> experimentExcels = experimentExcelService
				.queryListByExperimentId(pagerModel, experimentId);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("experimentExcels", experimentExcels);
		model.addAttribute("experimentExcelSearch", search);
		if (ajaxPost) {
			return view(output2ViewName);
		}
		return view(index2ViewName);
	}
	
	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String experimentExcelDetailed(Model model, String key) {
		ExperimentExcel experimentExcel = new ExperimentExcel();
		if (!StringUtils.isEmpty(key)) {
			experimentExcel = experimentExcelService.queryModel(key);
		}
		model.addAttribute("experimentExcel", experimentExcel);
		return view(detailedViewName);
	}
	
	@RequestMapping(value = detailed2ViewName, method = { RequestMethod.GET })
	public String experimentDetailed(Model model,PagerModel pagerModel, String key,boolean label,DataSearch search) {
		ExperimentExcel experimentExcel = new ExperimentExcel();
		if (!StringUtils.isEmpty(key)) {
			experimentExcel = experimentExcelService.queryModel(key);
		}
		
		search.setExcelId(key);
		System.out.println(key+"22222");
		pagerModel.setTotalAmount(dataService.queryCount(search));
		List<Data> datas = dataService.queryList(search, pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("dataSearch", search);
		model.addAttribute("datas", datas);
		model.addAttribute("experimentExcel", experimentExcel);
		return view(detailed2ViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, ExperimentExcel experimentExcel) {
		if (StringUtils.isEmpty(experimentExcel.getExcelId())) {
			experimentExcel.setExcelId(CommonUtils.getKeyId());
			//System.out.println(key+"5555555555555555555");
			//experimentExcel.setExperimentId(id);
			experimentExcel.setCreateId(getLoginModel().getLoginId());
			experimentExcel.setModelStatus(ModelStatus.add);
		} else {

			experimentExcel.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if (experimentExcelService.applyChange(experimentExcel)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		model.addAttribute("experimentExcel", experimentExcel);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (experimentExcelService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
	
	 
}